package com.hdax.dm.utils;

import cn.hutool.crypto.asymmetric.RSA;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtParser;
import io.jsonwebtoken.JwtParserBuilder;
import io.jsonwebtoken.Jwts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class Token {

    @Value("${jwt.rsa_public_key}")
    private String publicKey;

    /**
     * 对token进行解密
     * @param token 令牌
     * @return
     */
    public String token(String token){
        if (token==null||token.equals("")){
            return "0";
        }
        RSA rsa = new RSA(null,publicKey);
        JwtParserBuilder parserBuilder = Jwts.parserBuilder();
        parserBuilder.setSigningKey(rsa.getPublicKey());
        JwtParser parser = parserBuilder.build();
        Claims claims = parser.parseClaimsJws(token).getBody();
        return claims.getId();
    }
}